<template>
<div>
    <div class="container">
        <!-- 头部 -->
        <div class="nav">
            <div class="nav_wrap">
                <div class="quit" @click="$router.back()"></div>
                <span>书籍详情</span>
                <div class="nav_icon">
                    <router-link tag="a" to="/shelf"></router-link>
                    <i v-show="!isTop" @click="topShow"></i>
                    <i class="topnav" v-show="isTop" @click="topShow"></i>
                </div>
            </div>
        </div>
        <div class="wrap" v-show="isTop" @click.self="topShow">
            <div class="subnav">
                <ul>
                    <router-link tag="li" to="/">
                        <img src="../assets/images/tp_1.png" alt="">
                        <span>首页</span>
                    </router-link>
                    <router-link tag="li" to="/library">
                        <img src="../assets/images/tp_02.png" alt="">
                        <span>书库</span>
                    </router-link>
                    <router-link tag="li" to="/shelf">
                        <img src="../assets/images/tp_03.png" alt="">
                        <span>书架</span>
                    </router-link>
                    <router-link tag="li" to="/user">
                        <img src="../assets/images/tp_04.png" alt="">
                        <span>账户</span>
                    </router-link>
                    <router-link tag="li" to="/pay">
                        <img src="../assets/images/tp_05.png" alt="">
                        <span>充值</span>
                    </router-link>
                    <router-link tag="li" to="/search">
                        <img src="../assets/images/tp_06.png" alt="">
                        <span>搜索</span>
                    </router-link>
                </ul>
            </div>
        
        </div>
        <div class="selnav">
          <div class="sel" @click="select_show = true">{{ columns[pages] }}</div>
          <div class="sort"></div>
        </div>
        <!-- 选择器 -->
        <div class="select" @touchmove.prevent v-show="select_show">
            <van-picker title="标题" 
            show-toolbar 
            :columns="columns" 
            @confirm="confirm" 
            @cancel="cancel" 
             />
      
        </div>
        <!-- 文章列表 -->
        <div class="list_main bor_bot">
          <ul>
              <li
                v-for="(item,index) in titList" :key="index"
               @click="load(item)">
                <span>{{ item.navLabel | titSex}}</span>
                <em :class="[item.vip == 0 ?'free':'money']"></em>
                <!-- <em class="free"></em> -->
              </li>
          </ul>
          <div class="listBtn">
              <div class="run" @click="titShow(0)">上一页</div>
              <div class="pages" @click="select_show = true">{{ columns[pages] }}</div>
              <div class="run" @click="titShow">下一页</div>
          </div>
        </div>
      
        <!-- 搜索框 -->
        <router-link tag="div" to="/search" class="search">
            <div class="search_wrap">
                <input type="text">
                <button></button>
            </div>
        </router-link>
          <!-- 页脚信息 -->
          <div class="footer">
              <div class="foot_tit">
                  <ul>
                      <li>
                          <a href="javascript:;">首页</a>
                      </li>
                      <li>
                          <a href="javascript:;">书库</a>
                      </li>
                      <li>
                          <a href="javascript:;">书架</a>
                      </li>
                      <li>
                          <a href="javascript:;">账户</a>
                      </li>
                      <li>
                          <a href="javascript:;">客户端</a>
                      </li>
                  </ul>
              </div>
              <div class="foot_cont">
                  <div class="foot_info">
                      <p>客服QQ群：326738643</p>
                      <p>客服电话：010-86462015-800</p>
                      <p>©2020北京爱书客科技有限公司</p>
                      <p>京ICP备13037522号</p>
                  </div>
                  <div class="info_img">
                      <img src="../assets/images/ledu.png" alt="">
                      <span>乐读书城</span>
                  </div>
              </div>
          </div>

    </div>
</div>
</template>
    
<script>
    import { getListData } from '../api/list';
    import { getJsonData } from '../api/home';
// https://apis.netstart.cn/yunyuedu/reader/book/info.json?source_uuid=书籍ID
        export default {
            data(){
                return{
                    columns: [],
                    listInd:0,//对应书本的文章列表
                    select_show:false,
                    listAll:null,//列表总数据
                    titList:null,//列表要展示的数据
                    pages:0,//列表页数
                    isTop: false,//判断子菜单的展示
                    booksData:null,//书单列表
                    booksName:'',//书籍名称
                }
            },
            methods: {
                listCup(arr){//对数据做切割,[选项数据，列表数据]
                    this.listAll = arr;
                    let max = Math.ceil(arr.length / 100);
                    let array = [];

                    for(let i = 1; i <= max; i++){
                        array.push("第"+i+"页")
                    }

                    this.columns = array;
                    this.titCup(0);//列表数据初始化
                },
                titShow(bol){
                    if(bol){
                        //页数切换，下一页
                        if (this.columns.length - 1 > this.pages){
                            this.pages = this.pages + 1;
                            this.titCup(this.pages);
                            console.log(this.columns.length > this.pages);
                        }
                    }else{
                        //页数切换，上一页
                        if (0 < this.pages) {
                            this.pages = this.pages - 1;
                            this.titCup(this.pages);
                            console.log(this.columns.length > this.pages);
                        }
                    }
                },
                confirm(value, index){//选择器确认按钮
                    console.log(`${value}确认${index}`);
                    this.pages = index;
                    this.titCup(this.pages);//数据切换
                    this.select_show = false;
                },
                cancel() {//选择器取消按钮
                    console.log('取消');
                    this.select_show = false;
                },
                titCup(x){//列表数据切割，要展示的100条章节
                    x = x * 100;
                    this.titList = this.listAll.slice(x,x+100);
                    console.log('titCup',this.titList);
                },
                topShow() {
                    this.isTop = !this.isTop
                },
                load(item){//存储章节数据
                    let key = this.booksName;
                    console.log(key,'key');
                    localStorage.setItem(key, item.playOrder);

                    this.$router.push({
                        path: '/read',
                        query:{
                            ind: item.index,
                            len: this.listAll.length,
                            listInd: this.listInd,
                            vip: item.vip
                        }
                    })
                }
                
            },
            created() {
                this.listInd = this.$route.query.pages;
                console.log(this.listInd);
                getJsonData().then(data => {
                    this.booksName = data.data.books[this.listInd].title;
                    getListData().then(data => {
                       let arr = data.data[this.listInd].ncx.navMap.navPoint;
                       this.listCup(arr)
                    })

                    // getListData({ tocId: booksData }).then(data => {
                        
                    //     let booksList = data.ncx.navMap.navPoint;
                    //     this.listCup(booksList)
                    // })
                })
                
            },
            filters: {
                titSex(val) {
                    val = val.split("");
                    val.splice(0,1);
                    return val.join("")
                }
            }
        
    }
</script>

<style lang="scss" scoped>
.nav {
    background-color: #fff;

    .nav_wrap {
        padding: 0 14px;
        border-bottom: 1px solid #e0e0e0;
        display: flex;

        .quit {
            width: 10px;
            height: 45px;
            background-image: url('../assets/images/quit.png');
            background-repeat: no-repeat;
            background-size: 100%;
            background-position: center;
        }

        span {
            color: #333;
            font-size: 16px;
            display: block;
            text-align: center;
            line-height: 45px;
            width: 54%;
            padding-left: 70px;
        }

        .nav_icon {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 80px;

            a {
                display: block;
                width: 25px;
                height: 22px;
                background-image: url('../assets/images/book_ico1.png');
                background-repeat: no-repeat;
                background-size: 100%;
                background-position: center;
            }

            i {
                display: block;
                width: 20px;
                height: 20px;
                background-image: url('../assets/images/bookMore_ico1.png');
                background-size: 100%;
                background-position: center;

            }

            .topnav {
                background-image: url('../assets/images/tp_close.png');
            }
        }
    }
}

.wrap {
    position: fixed;
    top: 45px;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, .8);
    z-index: 10;
    &>.subnav {
        width: 100%;
        background-color: #fff;
        padding-bottom: 18px;

        ul {
            display: flex;
            flex-wrap: wrap;

            li {
                width: 25%;
                text-align: center;
                padding-top: 20px;

                img {
                    width: 40px;
                    height: 40px;
                    margin: 0 auto 8px auto;
                }

                span {
                    font-size: 13px;
                    color: #666;
                }
            }
        }
    }

}
.selnav{
    border-bottom: 1px solid #f1f1f1;
    display: flex;
    padding: 15px 14px;
    &>.sel{
        width: 75px;
        height: 25px;
        line-height: 25px;
        border-radius: 6px;
        font-size: 14px;
        color:#333;
        text-align: center;
        padding-right: 23px;
        background-image: url('../assets/images/downJt.png');
        background-repeat: no-repeat;
        background-size: 10px;
        background-position: 75px center;
        border: 1px solid #999999;
    }
}
.list_main{
    ul{
        padding: 0 14.5px;
        li{
            display: flex;
            height: 57px;
            line-height: 57px;
            span{
                display: block;
                width: 305px;
                text-indent: 12px;
                color: #666;
                font-size: 14.5px;
                overflow: hidden;
            }
            em{
                display: block;
                position: relative;
                width: 40px;
            }
            .free{
                &::before{
                    content:'免费';
                    font-size:13px;
                    color: #999;
                    font-style: normal;
                    text-align: center;
                }
            }
            .money{
                &::before{
                    content:"";
                    width: 41px;
                    height: 57px;
                    display: block;
                    background-image: url('../assets/images/moneyIco.png');
                    background-repeat: no-repeat;
                    background-position: center;
                    background-size: 18px;
                    border-bottom: 1px solid #ebebeb;
                }
            }
        }
    }
    .listBtn{
        height: 88px;
        padding: 0 24px;
        display: flex;
        text-align: center;
        justify-content: space-between;
        align-items: center;
        .run{
            width: 90px;
            height: 37px;
            color: #fff;
            background-color: #ea4136;
            line-height: 37px;
        }
        .pages{
            width: 110px;
            height: 35px;
            line-height: 35px;
            color: #ea4136;
            border: 1px solid #ea4136;
        }
        div{
            border-radius: 5px;
        }
    }
}
.bor_bot {
    border-bottom: 10px solid #eaeaea;
}
.search {
    padding: 10px 14px;

    .search_wrap {
        display: flex;
        padding: 4px 0 4px 17px;
        border: 1px solid #dbdbdb;
        height: 32px;
        border-radius: 20px;
        overflow: hidden;

        &>input {
            border: 0px;
            outline: none;
            flex: 6;
            color: #999;
        }

        &>button {
            flex: 1;
            background-image: url('../assets/images/searchBar.png');
            background-repeat: no-repeat;
            border: 0px;
            background-color: #fff;
            position: relative;
            border-left: 1px solid #dbdbdb;
            background-size: 18px;
            background-position: center;
        }
    }
}
.footer {
    padding: 16px 0px 32px 0px;
    background-color: #1c1a1a;

    .foot_tit {
        padding-bottom: 15px;

        ul {
            display: flex;

            li {
                flex: 1;

                a {
                    display: block;
                    font-size: 16px;
                    color: #b6b6b6;
                    text-align: center;
                    border-right: 1px solid #999999;

                }

                &:last-of-type {
                    a {
                        border: 0px;
                    }
                }
            }
        }
    }

    .foot_cont {
        display: flex;
        justify-content: space-between;
        padding: 0 16px;

        .foot_info {
            p {
                color: #b6b6b6;
                font-size: 12px;
                line-height: 28px;
            }
        }

        .info_img {
            img {
                width: 98px;
                height: 98px;
            }

            span {
                font-size: 12px;
                display: block;
                text-align: center;
                color: #b6b6b6;
                padding-top: 3px;
            }
        }
    }
}
.select{
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba($color: #000000, $alpha: 0.7);
    z-index: 10;
    .van-picker{
        top: 54%;
    }
}
</style>